home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / bildschirmschoner / bserver_v1.5 / sources.lha / Sources / lib / libsources / client_library.c < prev    next >
C/C++ Source or Header  |  1995-11-08  |  24KB  |  980 lines

  1. ; /*
  2. echo "Compiling shared library"
  3. if not exists client.library.o
  4.   sc libcode nostackcheck constlib streq strmerge ignore=73 define=SHARED optimize optsize optpeep client_library.c DEBUG=LINE
  5. endif
  6. if not exists PTPlay.o
  7.   asm -cc -m0 PTPlay.s
  8. endif
  9. slink LIBFD client.fd TO ///Libs/client.library FROM LIB:libent.o LIB:libinit.o client_library.o Decrunch30.o PTPlay.o LIB lib:sc.lib lib:amiga.lib libVersion 1 libRevision 7 libid "client.library 1.8 (8.11.95) Copyright 1994-1995 by Stefano Reksten" SD SC NOICONS STRIPDEBUG
  10. copy ///Libs/client.library LIBS:
  11.  
  12. ;echo "Compiling linked library"
  13. ;sc parms=registers optimize optsize constlib nostackcheck streq IGNORE=73 define=LINKED client_library.c
  14. ;oml /client.lib r client_library.o
  15.  
  16. quit
  17.  
  18.  Client.lib  1.8
  19.  Copyright © 1994-1995 by Stefano Reksten of 3AM - The Three Amigos!!!
  20.  All rights reserved.
  21.  
  22. */
  23.  
  24.  
  25. #include <exec/memory.h>
  26. #include <intuition/screens.h>
  27. #include <intuition/intuitionbase.h>
  28. #include <graphics/displayinfo.h>
  29. #include <graphics/gfxbase.h>
  30. #include <graphics/gfxmacros.h>
  31. #include <libraries/iffparse.h>
  32. #include <devices/audio.h>
  33. #include <devices/timer.h>
  34. #include <hardware/custom.h>
  35. #include <hardware/dmabits.h>
  36. #include <hardware/intbits.h>
  37.  
  38. #include <proto/exec.h>
  39. #include <proto/dos.h>
  40. #include <proto/intuition.h>
  41. #include <proto/graphics.h>
  42. #include <proto/iffparse.h>
  43. #include <proto/diskfont.h>
  44. #include <string.h>
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47.  
  48. #include "//include/server.h"
  49.  
  50. #ifdef SHARED
  51.  
  52.  /****************************
  53.  *                           *
  54.  * "LOW-LEVEL" COMMUNICATION *
  55.  *        PRIMITIVES         *
  56.  *                           *
  57.  ****************************/
  58.  
  59. struct GfxBase *GfxBase;
  60. struct IntuitionBase *IntuitionBase;
  61. struct Library *IFFParseBase, *DiskfontBase;
  62. struct DosLibrary *DOSBase;
  63.  
  64. int __saveds __asm __UserLibInit( register __a6 struct MyLibrary *libbase )
  65. {
  66. int result = 1;
  67.  
  68. if ( GfxBase = (struct GfxBase *)OpenLibrary( "graphics.library", 37L ) )
  69.     {
  70.     if ( IntuitionBase = (struct IntuitionBase *)OpenLibrary( "intuition.library", 37L ) )
  71.         {
  72.         if ( DOSBase = (struct DosLibrary *)OpenLibrary( "dos.library", 37L ) )
  73.             {
  74.             if ( IFFParseBase = OpenLibrary( "iffparse.library", 37L ) )
  75.                 {
  76.                 if ( DiskfontBase = OpenLibrary( "diskfont.library", 37L ) )
  77.                     result = 0;
  78.                 else CloseLibrary( IFFParseBase );
  79.                 }
  80.             else CloseLibrary( (struct Library *)DOSBase );
  81.             }
  82.         else CloseLibrary( (struct Library *)IntuitionBase );
  83.         }
  84.     else CloseLibrary( (struct Library *)GfxBase );
  85.     }
  86. return result;
  87. }
  88.  
  89. void __saveds __asm __UserLibCleanup( register __a6 struct MyLibrary *libbase )
  90. {
  91. CloseLibrary( DiskfontBase );
  92. CloseLibrary( IFFParseBase );
  93. CloseLibrary( (struct Library *)DOSBase );
  94. CloseLibrary( (struct Library *)GfxBase );
  95. CloseLibrary( (struct Library *)IntuitionBase );
  96. }
  97.  
  98.  
  99. static struct ClientMessage *__asm __saveds __AllocClientMessage( register __d0 ULONG action,register __a1 struct MsgPort *CommunicationPort )
  100. {
  101. struct ClientMessage *bmsg;
  102.  
  103. if ( bmsg = AllocVec( sizeof(struct ClientMessage), MEMF_PUBLIC | MEMF_CLEAR ) )
  104.     {
  105.     bmsg->cm_Msg.mn_Node.ln_Type = NT_MESSAGE;
  106.     bmsg->cm_Msg.mn_ReplyPort = CommunicationPort;
  107.     bmsg->cm_Msg.mn_Length = sizeof(struct ClientMessage);
  108.     bmsg->cm_Action = action;
  109.     }
  110. return( bmsg );
  111. }
  112.  
  113.  
  114. static void __asm __saveds __FreeClientMessage( register __a1 struct ClientMessage *bmsg )
  115. {
  116. FreeVec( bmsg );
  117. }
  118.  
  119.  
  120. BOOL __asm __saveds __SendClientMsg( register __d0 ULONG action, register __a1 struct MsgPort *CommunicationPort )
  121. {
  122. struct MsgPort *ServerPort;
  123. struct ClientMessage *bmsg, *replymsg = NULL;
  124. BOOL talked = FALSE;
  125.  
  126. if ( bmsg = __AllocClientMessage( action, CommunicationPort ) )
  127.     {
  128.     Forbid();
  129.     if ( ServerPort = FindPort( SERVERPORTNAME ) )
  130.         PutMsg( ServerPort, (struct Message *)bmsg );
  131.     Permit();
  132.  
  133.     if ( ServerPort )
  134.         {
  135.         while ( !replymsg )
  136.             {
  137.             WaitPort( CommunicationPort );
  138.             replymsg = (struct ClientMessage *)GetMsg( CommunicationPort );
  139.             }
  140.         talked = TRUE;
  141.         }
  142.     __FreeClientMessage( bmsg );
  143.     }
  144. return( talked );
  145. }
  146.  
  147.  
  148. ULONG __asm __saveds __GetServerCommand( register __a1 struct MsgPort *CommunicationPort )
  149. {
  150. register struct ServerMessage *msg;
  151. register ULONG command;
  152.  
  153. if ( msg = (struct ServerMessage *)GetMsg( CommunicationPort ) )
  154.     {
  155.     command = msg->sm_Command;
  156.     ReplyMsg( (struct Message *)msg );
  157.     return( command );
  158.     }
  159. else
  160.     return( COMMAND_IDLE );
  161. }
  162.  
  163.  
  164. ULONG __asm __saveds __WaitServerCommand( register __a1 struct MsgPort *CommunicationPort )
  165. {
  166. WaitPort( CommunicationPort );
  167. return( __GetServerCommand( CommunicationPort ) );
  168. }
  169.  
  170.  
  171. struct DisplayIDInformation * __asm __saveds __OpenCommunication( register __a1 struct MsgPort **mp )
  172. {
  173. struct MsgPort *ServerPort;
  174. struct MsgPort *CommunicationPort;
  175. struct ClientMessage *cm, *replymsg = NULL;
  176. struct DisplayIDInformation *dinfo;
  177. BOOL talked = FALSE;
  178.  
  179. if ( CommunicationPort = CreateMsgPort() )
  180.     {
  181.     if ( cm = __AllocClientMessage( ACTION_ARRIVED, CommunicationPort ) )
  182.         {
  183.         Forbid();
  184.         if ( ServerPort = FindPort( SERVERPORTNAME ) )
  185.             PutMsg( ServerPort, (struct Message *)cm );
  186.             Permit();
  187.  
  188.         if ( ServerPort )
  189.             {
  190.             while ( !replymsg )
  191.                 {
  192.                 WaitPort( CommunicationPort );
  193.                 replymsg = (struct ClientMessage *)GetMsg( CommunicationPort );
  194.                 }
  195.             talked = TRUE;
  196.             dinfo = replymsg->DInfo;
  197.             *mp = CommunicationPort;
  198.             }
  199.  
  200.         __FreeClientMessage( cm );
  201.         if ( talked )
  202.             {
  203.             SetTaskPri( FindTask( NULL ), -5 );
  204.             return dinfo;
  205.             }
  206.         }
  207.     DeleteMsgPort( CommunicationPort );
  208.     }
  209. return NULL;
  210. }
  211.  
  212.  
  213.  /*****************
  214.  *                *
  215.  * User functions *
  216.  *                *
  217.  *****************/
  218.  
  219. BOOL __asm __saveds CheckAA( void )
  220. {
  221. return (BOOL)((GfxBase->LibNode.lib_Version >= 39) && (GfxBase->ChipRevBits0 & 1<<GFXB_AA_ALICE));
  222. }
  223.  
  224.  
  225. struct Screen * __asm __saveds CloneFrontmostScreen( register __d0 UBYTE brightness )
  226. {
  227. register struct Screen *cloned, *fm;
  228. register int depth, n;
  229. register ULONG *RGBTriplets, *copy_of_RGBTriplets;
  230.  
  231. Forbid();
  232. fm = IntuitionBase->FirstScreen;
  233. depth = fm->RastPort.BitMap->Depth;
  234. if ( RGBTriplets = AllocVec( 3088, MEMF_ANY|MEMF_CLEAR ) )
  235.     {
  236.     copy_of_RGBTriplets = (ULONG *)((ULONG)RGBTriplets + 4);
  237.     *RGBTriplets = ( 1 << depth ) << 16;
  238.     cloned = OpenScreenTags( NULL,
  239.         SA_Left, fm->LeftEdge,
  240.         SA_Top, 0,
  241.         SA_Width, fm->Width,
  242.         SA_Height, fm->Height,
  243.         SA_Depth, depth,
  244.         SA_DisplayID, GetVPModeID( &fm->ViewPort ),
  245.         SA_Font, fm->Font,
  246.         SA_Behind, TRUE,
  247.         TAG_END );
  248.     if ( cloned )
  249.         {
  250.         GetRGB32( fm->ViewPort.ColorMap, 0, 1<<depth, copy_of_RGBTriplets );
  251.         for ( n = 0; n < 3 << depth; n++ )
  252.             {
  253.             *copy_of_RGBTriplets = (((*copy_of_RGBTriplets) >> 24) * brightness / 100 ) << 24;
  254.             copy_of_RGBTriplets++;
  255.             }
  256.         LoadRGB32( &cloned->ViewPort, RGBTriplets );
  257.         BltBitMap( fm->RastPort.BitMap, 0, 0,
  258.                    cloned->RastPort.BitMap, 0, 0,
  259.                    fm->Width, fm->Height, 0xC0, 0xFF, NULL );
  260.         ScreenToFront( cloned );
  261.         }
  262.     FreeVec( RGBTriplets );
  263.     }
  264. Permit();
  265. return( cloned );
  266. }
  267.  
  268.  
  269. ULONG __asm __saveds DarkestColorIndex( register __a1 struct Screen *scr )
  270. {
  271. register ULONG *RGBTriplets, *copy_of_RGBTriplets;
  272. register ULONG color_index = 0, minbri = 0xFFFFFFFF, bri, n;
  273. register ULONG r, g, b;
  274. register ULONG colors_to_get = 1L<<scr->RastPort.BitMap->Depth;
  275.  
  276. if ( RGBTriplets = AllocVec( 3072, MEMF_ANY|MEMF_CLEAR ) )
  277.     {
  278.     copy_of_RGBTriplets = RGBTriplets;
  279.  
  280.     GetRGB32( scr->ViewPort.ColorMap, 0, colors_to_get, (APTR)RGBTriplets );
  281.  
  282.     for ( n = 0; n < colors_to_get; n++ )
  283.         {
  284.         r = (*copy_of_RGBTriplets++) >> 24;
  285.         g = (*copy_of_RGBTriplets++) >> 24;
  286.         b = (*copy_of_RGBTriplets++) >> 24;
  287.         bri = r * r + g * g + b * b;
  288.         if ( bri < minbri )
  289.             {
  290.             color_index = n;
  291.             minbri = bri;
  292.             }
  293.         }
  294.  
  295.     FreeVec( RGBTriplets );
  296.     return color_index;
  297.     }
  298. else return 0;
  299. }
  300.  
  301.  
  302. ULONG __asm __saveds BrightestColorIndex( register __a1 struct Screen *scr )
  303. {
  304. register ULONG *RGBTriplets, *copy_of_RGBTriplets;
  305. register ULONG color_index = 0, maxbri = 0, bri, n;
  306. register ULONG r, g, b;
  307. register ULONG colors_to_get = 1L<<scr->RastPort.BitMap->Depth;
  308.  
  309. if ( RGBTriplets = AllocVec( 3072, MEMF_ANY|MEMF_CLEAR ) )
  310.     {
  311.     copy_of_RGBTriplets = RGBTriplets;
  312.  
  313.     GetRGB32( scr->ViewPort.ColorMap, 0, colors_to_get, (APTR)RGBTriplets );
  314.  
  315.     for ( n = 0; n < colors_to_get; n++ )
  316.         {
  317.         r = (*copy_of_RGBTriplets++) >> 24;
  318.         g = (*copy_of_RGBTriplets++) >> 24;
  319.         b = (*copy_of_RGBTriplets++) >> 24;
  320.         bri = r * r + g * g + b * b;
  321.         if ( bri > maxbri )
  322.             {
  323.             color_index = n;
  324.             maxbri = bri;
  325.             }
  326.         }
  327.  
  328.     FreeVec( RGBTriplets );
  329.     return color_index;
  330.     }
  331. else return 0;
  332. }
  333.  
  334.  
  335. struct Screen * __asm __saveds GetDeeperFrontmostScreen( register __d0 UBYTE brightness, register __d1 UBYTE imagedepth )
  336. {
  337. register struct Screen *cloned = NULL, *fm;
  338. register int depth, fmdepth;
  339. register ULONG *RGBTriplets, *copy_of_RGBTriplets, vpmode;
  340.  
  341. Forbid();
  342. fm = IntuitionBase->FirstScreen;
  343. fmdepth = fm->RastPort.BitMap->Depth;
  344. vpmode = GetVPModeID( &fm->ViewPort );
  345.  
  346. if ( fmdepth >= 7 || vpmode & HAM || vpmode & EXTRA_HALFBRITE )
  347.     {
  348.     Permit();
  349.     return NULL;
  350.     }
  351.  
  352. if ( imagedepth <= fmdepth )
  353.     depth = fmdepth + 1;
  354. else
  355.     depth = imagedepth + 1;
  356.  
  357. if ( RGBTriplets = AllocVec( 3088, MEMF_ANY|MEMF_CLEAR ) )
  358.     {
  359.     copy_of_RGBTriplets = (ULONG *)((ULONG)RGBTriplets + 4);
  360.     if ( cloned = OpenScreenTags( NULL,
  361.         SA_Left, fm->LeftEdge,
  362.         SA_Width, fm->Width,
  363.         SA_Height, fm->Height,
  364.         SA_Depth, depth,
  365.         SA_DisplayID, vpmode,
  366.         SA_Font, fm->Font,
  367.         SA_Behind, TRUE,
  368.         TAG_END ) )
  369.         {
  370.         register UWORD *plane;
  371.         register ULONG n;
  372.  
  373.         GetRGB32( fm->ViewPort.ColorMap, 0, 1<<fmdepth, copy_of_RGBTriplets );
  374.         for ( n = 0; n < 3 << fmdepth; n++ )
  375.             {
  376.             *copy_of_RGBTriplets = (((*copy_of_RGBTriplets) >> 24) * brightness / 100 ) << 24;
  377.             copy_of_RGBTriplets++;
  378.             }
  379.         *RGBTriplets = (1L<<fmdepth+16) + (1L<<depth-1);
  380.         LoadRGB32( &cloned->ViewPort, RGBTriplets );
  381.         *RGBTriplets = (1L<<fmdepth+16);
  382.         LoadRGB32( &cloned->ViewPort, RGBTriplets );
  383.  
  384.         BltBitMap( fm->RastPort.BitMap, 0, 0,
  385.                    cloned->RastPort.BitMap, 0, 0,
  386.                    fm->Width, fm->Height, 0xC0, 0xFF, NULL );
  387.  
  388.         plane = (UWORD *)cloned->RastPort.BitMap->Planes[depth-1];
  389.         n = ( cloned->RastPort.BitMap->Rows * cloned->RastPort.BitMap->BytesPerRow ) >> 1;
  390.         while( n-- )
  391.             *plane++ = 0xFFFF;
  392.  
  393.         ScreenToFront( cloned );
  394.         }
  395.     FreeVec( RGBTriplets );
  396.     }
  397. Permit();
  398. return( cloned );
  399. }
  400.  
  401.  
  402. /***********************************************/
  403. /*       New sound supporting functions        */
  404. /***********************************************/
  405.  
  406. static UBYTE whichChannel[4] = { 1, 2, 4, 8 };
  407. static BYTE codeToDelta[16] = { -34, -21, -13, -8, -5, -3, -2, -1, 0, 1, 2, 3, 5, 8, 13, 21 };
  408.  
  409. #define ID_VHDR MAKE_ID('V','H','D','R')
  410. #define ID_BODY MAKE_ID('B','O','D','Y')
  411. #define ID_8SVX MAKE_ID('8','S','V','X')
  412.  
  413. typedef struct {
  414.     ULONG oneShotHiSamples, repeatHiSamples, samplesPerCycle;
  415.     UWORD samplesPerSec;
  416.     UBYTE ctOctave, sCompression;
  417.     LONG  volume; } Voice8Header;
  418.  
  419. #define sCmpFibDelta 1
  420.  
  421.  
  422. Sound * __asm __saveds Open8SVX( register __a1 char *iffname )
  423. {
  424. Sound *svx;
  425. register struct IFFHandle *ih;
  426. BOOL error = TRUE;
  427. LONG ifferr;
  428.  
  429. if ( !iffname )
  430.     return 0L;
  431.  
  432. if ( svx = AllocVec( sizeof(Sound), MEMF_ANY|MEMF_CLEAR ) )
  433.     {
  434.     if ( ih = AllocIFF() )
  435.         {
  436.         if ( ih->iff_Stream = (ULONG)Open( iffname, MODE_OLDFILE ) )
  437.             {
  438.             InitIFFasDOS( ih );
  439.             if ( 0 == OpenIFF( ih, IFFF_READ ) )
  440.                 {
  441.                 PropChunk( ih, ID_8SVX, ID_VHDR );
  442.                 PropChunk( ih, ID_8SVX, ID_BODY );
  443.                 StopOnExit( ih, ID_8SVX, ID_FORM );
  444.                 ifferr = ParseIFF( ih, IFFPARSE_SCAN );
  445.                 if ( !ifferr || ifferr == IFFERR_EOF || ifferr == IFFERR_EOC )
  446.                     {
  447.                     register struct StoredProperty *sp;
  448.                     register Voice8Header *vh;
  449.                     UBYTE *bdy;
  450.  
  451.                     if ( sp = FindProp( ih, ID_8SVX, ID_VHDR ) )
  452.                         {
  453.                         vh = (Voice8Header *)sp->sp_Data;
  454.                         svx->Volume = 64 * vh->volume / 65536;
  455.                         svx->Period = vh->samplesPerSec;
  456.                         svx->DataLength = vh->oneShotHiSamples + vh->repeatHiSamples;
  457.  
  458.                         if ( sp = FindProp( ih, ID_8SVX, ID_BODY ) )
  459.                             {
  460.                             bdy = (UBYTE *)sp->sp_Data;
  461.                             if ( vh->sCompression == sCmpFibDelta )
  462.                                 {
  463.                                 if ( svx->AudioData = AllocVec( 2 * ( svx->DataLength - 2 ), MEMF_CHIP ) )
  464.                                     {
  465.                                     LONG i;
  466.                                     BYTE d, x = bdy[1];
  467.  
  468.                                     for ( i = 0; i < svx->DataLength << 1; i++ )
  469.                                         {
  470.                                         d = bdy[i>>1];
  471.                                         if ( i & 1 )
  472.                                             d &= 0x0F;
  473.                                         else
  474.                                             d >>= 4;
  475.                                         x += codeToDelta[d];
  476.                                         svx->AudioData[i] = x;
  477.                                         }
  478.                                     error = FALSE;
  479.                                     }
  480.                                 }
  481.                             else
  482.                                 if ( svx->AudioData = AllocVec( svx->DataLength, MEMF_CHIP ) )
  483.                                     {
  484.                                     CopyMem( bdy, svx->AudioData, svx->DataLength );
  485.                                     error = FALSE;
  486.                                     }
  487.                             }
  488.                         }
  489.                     }
  490.                 }
  491.             Close( ih->iff_Stream );
  492.             }
  493.         FreeIFF( ih );
  494.         }
  495.     if ( error )
  496.         {
  497.         FreeVec( svx );
  498.         svx = NULL;
  499.         }
  500.     }
  501. return svx;
  502. }
  503.  
  504.  
  505. void __asm __saveds Play8SVX( register __a1 Sound *svx, register __d0 UBYTE volume )
  506. {
  507. struct MsgPort *MP;
  508. BOOL SoundPlayed = FALSE;
  509.  
  510. if ( !svx )
  511.     return;
  512.  
  513. if ( MP = CreatePort( NULL, NULL ) )
  514.     {
  515.     struct IOAudio *AudioIO;
  516.     if ( AudioIO = (struct IOAudio *)AllocVec( sizeof(struct IOAudio), MEMF_PUBLIC | MEMF_CLEAR ) )
  517.         {
  518.         AudioIO->ioa_Request.io_Message.mn_ReplyPort   = MP;
  519.         AudioIO->ioa_Request.io_Message.mn_Node.ln_Pri = 127;
  520.         AudioIO->ioa_Request.io_Command                = ADCMD_ALLOCATE;
  521.         AudioIO->ioa_Request.io_Flags                  = ADIOF_NOWAIT;
  522.         AudioIO->ioa_AllocKey                          = 0;
  523.         AudioIO->ioa_Data                              = whichChannel;
  524.         AudioIO->ioa_Length                            = sizeof(whichChannel);
  525.  
  526.         if ( !OpenDevice( "audio.device", 0L, (struct IORequest *)AudioIO, 0L ) )
  527.             {
  528.             AudioIO->ioa_Request.io_Message.mn_ReplyPort   = MP;
  529.             AudioIO->ioa_Request.io_Command                = CMD_WRITE;
  530.             AudioIO->ioa_Request.io_Flags                  = ADIOF_PERVOL;
  531.             AudioIO->ioa_Data                              = svx->AudioData;
  532.             AudioIO->ioa_Length                            = svx->DataLength;
  533.             AudioIO->ioa_Period                            = (( GfxBase->DisplayFlags & PAL ) ? 3546895L : 3579545L) / svx->Period;
  534.             AudioIO->ioa_Volume                            = volume;
  535.             AudioIO->ioa_Cycles                            = 1;
  536.  
  537.             BeginIO( (struct IORequest *)AudioIO );
  538.             WaitPort( MP );
  539.             SoundPlayed = TRUE;
  540.             CloseDevice( (struct IORequest *)AudioIO );
  541.             }
  542.         DeleteExtIO( (struct IORequest *)AudioIO );
  543.         }
  544.  
  545.     if ( !SoundPlayed )
  546.         {
  547.         struct timerequest *TimerIO;
  548.         if ( TimerIO = (struct timerequest *)CreateExtIO( MP, sizeof(struct timerequest) ) )
  549.             {
  550.             if ( !OpenDevice( "timer.device", UNIT_MICROHZ, TimerIO, 0 ) )
  551.                 {
  552.                 TimerIO->tr_node.io_Command = TR_ADDREQUEST;
  553.                 TimerIO->tr_time.tv_secs = svx->DataLength / svx->Period;
  554.                 TimerIO->tr_time.tv_micro = svx->DataLength % svx->Period;
  555.  
  556.                 DoIO( (struct IORequest *)TimerIO );
  557.                 CloseDevice( (struct IORequest *)TimerIO );
  558.                 }
  559.             DeleteExtIO( (struct IORequest *)TimerIO );
  560.             }
  561.         }
  562.     DeletePort( MP );
  563.     }
  564. }
  565.  
  566.  
  567. static void __asm __saveds Complete8SVX( register __a1 Sound *svx )
  568. {
  569. if ( svx->AudioIO )
  570.     {
  571.     register struct MsgPort *mp = svx->AudioIO->ioa_Request.io_Message.mn_ReplyPort;
  572.     CloseDevice( (struct IORequest *)svx->AudioIO );
  573.     DeleteExtIO( (struct IORequest *)svx->AudioIO );
  574.     DeletePort( mp );
  575.     svx->AudioIO = NULL;
  576.     }
  577. }
  578.  
  579.  
  580. void __asm __saveds PlayAsynch8SVX( register __a1 Sound *svx, register __d0 UBYTE volume )
  581. {
  582. struct MsgPort *AudioMP;
  583. struct IOAudio *AudioIO;
  584.  
  585. if ( !svx )
  586.     return;
  587.  
  588. Complete8SVX( svx );
  589.  
  590. if ( AudioMP = CreatePort( NULL, NULL ) )
  591.     {
  592.     if ( AudioIO = (struct IOAudio *)AllocVec( sizeof(struct IOAudio), MEMF_PUBLIC | MEMF_CLEAR ) )
  593.         {
  594.         AudioIO->ioa_Request.io_Message.mn_ReplyPort   = AudioMP;
  595.         AudioIO->ioa_Request.io_Message.mn_Node.ln_Pri = 127;
  596.         AudioIO->ioa_Request.io_Command                = ADCMD_ALLOCATE;
  597.         AudioIO->ioa_Request.io_Flags                  = ADIOF_NOWAIT;
  598.         AudioIO->ioa_AllocKey                          = 0;
  599.         AudioIO->ioa_Data                              = whichChannel;
  600.         AudioIO->ioa_Length                            = sizeof(whichChannel);
  601.  
  602.         if ( !OpenDevice( "audio.device", 0L, (struct IORequest *)AudioIO, 0L ) )
  603.             {
  604.             AudioIO->ioa_Request.io_Message.mn_ReplyPort   = AudioMP;
  605.             AudioIO->ioa_Request.io_Command                = CMD_WRITE;
  606.             AudioIO->ioa_Request.io_Flags                  = ADIOF_PERVOL;
  607.             AudioIO->ioa_Data                              = svx->AudioData;
  608.             AudioIO->ioa_Length                            = svx->DataLength;
  609.             AudioIO->ioa_Period                            = (( GfxBase->DisplayFlags & PAL ) ? 3546895L : 3579545L) / svx->Period;
  610.             AudioIO->ioa_Volume                            = volume;
  611.             AudioIO->ioa_Cycles                            = 1;
  612.  
  613.             svx->AudioIO = AudioIO;
  614.             BeginIO( (struct IORequest *)AudioIO );
  615.             }
  616.         else
  617.             {
  618.             DeleteExtIO( (struct IORequest *)AudioIO );
  619.             DeletePort( AudioMP );
  620.             }
  621.         }
  622.     else DeletePort( AudioMP );
  623.     }
  624. }
  625.  
  626.  
  627. void __asm __saveds Close8SVX( register __a1 Sound *svx )
  628. {
  629. if ( svx )
  630.     {
  631.     Complete8SVX( svx );
  632.     FreeVec( svx->AudioData );
  633.     FreeVec( svx );
  634.     }
  635. }
  636.  
  637.  
  638. /** Module stuff **/
  639.  
  640. void __asm __saveds InitModule( register __a1 Module * );
  641. BOOL __asm __saveds PlayModule( register __a1 Module * );
  642. void __asm __saveds StopModule( register __a1 Module * );
  643. void __asm __saveds FreeModule( register __a1 Module * );
  644. char * __asm __saveds GetEquilizers( register __a1 Module * );
  645.  
  646. Module * __asm __saveds OpenModule( register __a1 char *modulename )
  647. {
  648. BPTR handle, lock;
  649. Module *mod;
  650. ULONG *mem;
  651. struct FileInfoBlock fib;
  652.  
  653. if ( mod = AllocVec( sizeof(Module), MEMF_ANY | MEMF_CLEAR ) )
  654.     {
  655.     if ( lock = Lock( modulename, ACCESS_READ ) )
  656.         {
  657.         if ( Examine( lock, &fib ) && ( fib.fib_DirEntryType < 0 ) )
  658.             {
  659.             if ( handle = Open( modulename, MODE_OLDFILE ) )
  660.                 {
  661.                 if ( mod->m_Data = mem = AllocVec( fib.fib_Size, MEMF_CHIP ) )
  662.                     {
  663.                     if ( Read( handle, mem, fib.fib_Size ) == fib.fib_Size )
  664.                         {
  665.                         if ( mem[270] == MAKE_ID( 'M','.','K','.' ) )
  666.                             {
  667.                             InitModule( mod );
  668.                             Close( handle );
  669.                             UnLock( lock );
  670.                             return mod;
  671.                             }
  672.                         }
  673.                     FreeVec( mem );
  674.                     }
  675.                 Close( handle );
  676.                 }
  677.             }
  678.         UnLock( lock );
  679.         }
  680.     FreeVec( mod );
  681.     }
  682. return NULL;
  683. }
  684.  
  685.  
  686. void __asm __saveds InitModule( register __a1 Module *module )
  687. {
  688. extern void __asm pt_InitMusic( register __a0 APTR mod_data );
  689. extern void __interrupt GoPlay( void );
  690. if ( module )
  691.     {
  692.     if ( module->m_IntInstalled )
  693.         StopModule( module );
  694.     module->m_Int.is_Data = module->m_Data;
  695.     module->m_Int.is_Code = GoPlay;
  696.     module->m_Int.is_Node.ln_Pri = 5;
  697.     module->m_Int.is_Node.ln_Type = NT_INTERRUPT;
  698.     module->m_Int.is_Node.ln_Name = "BServer_PT_Int";
  699.     pt_InitMusic( module->m_Data );
  700.     }
  701. }
  702.  
  703.  
  704. BOOL __asm __saveds PlayModule( register __a1 Module *module )
  705. {
  706. if ( module && (module->m_IntInstalled == FALSE) )
  707.     {
  708.     struct IOAudio AudioIO;
  709.     struct MsgPort *mp;
  710.     UBYTE channel[] = { 1+2+4+8 }; /*All the channels */
  711.  
  712.     if ( mp = CreateMsgPort() )
  713.         {
  714.         memset( &AudioIO, 0, sizeof(struct IOAudio) );
  715.         AudioIO.ioa_Request.io_Message.mn_ReplyPort = mp;
  716.         AudioIO.ioa_Request.io_Message.mn_Node.ln_Pri = -80;
  717.         AudioIO.ioa_Request.io_Command = ADCMD_ALLOCATE;
  718.         AudioIO.ioa_Request.io_Flags = ADIOF_NOWAIT;
  719.         AudioIO.ioa_Data = channel;
  720.         AudioIO.ioa_Length = sizeof(channel);
  721.  
  722.         if ( !OpenDevice( "audio.device", 0L, (struct IORequest *)&AudioIO, 0L ) )
  723.             {
  724.             CloseDevice( &AudioIO );
  725.             AddIntServer( INTB_VERTB, &module->m_Int );
  726.             module->m_IntInstalled = TRUE;
  727.             }
  728.         DeleteMsgPort( mp );
  729.         }
  730.     }
  731. return module->m_IntInstalled;
  732. }
  733.  
  734.  
  735. void __asm __saveds StopModule( register __a1 Module *module )
  736. {
  737. extern void __asm pt_StopMusic( void );
  738. if ( module && (module->m_IntInstalled == TRUE) )
  739.     {
  740.     RemIntServer( INTB_VERTB, &module->m_Int );
  741.     module->m_IntInstalled = FALSE;
  742.     pt_StopMusic();
  743.     }
  744. }
  745.  
  746.  
  747. void __asm __saveds FreeModule( register __a1 Module *module )
  748. {
  749. if ( module != NULL )
  750.     {
  751.     if ( module->m_IntInstalled )
  752.         StopModule( module );
  753.     FreeVec( module->m_Data );
  754.     FreeVec( module );
  755.     }
  756. }
  757.  
  758.  
  759. char * __asm __saveds GetEquilizers( register __a1 Module *mod )
  760. {
  761. extern far char pt_Equilizers[];
  762. return pt_Equilizers;
  763. }
  764.  
  765. /*************************************/
  766. /*                                   */
  767. /*  Arguments passing related stuff  */
  768. /*                                   */
  769. /*************************************/
  770.  
  771. char * __asm GetArgString( register __a0 char *line, register __a1 char *hdr, register __d0 char *store )
  772. {
  773. char *s, *ln = line;
  774.  
  775. if ( !*line || !*hdr )
  776.     return 0L;
  777.  
  778. while ( 1 )
  779.     {
  780.     s = hdr;
  781.  
  782.     while( *ln && (*ln == ' ' || *ln == '\t') )
  783.         ln++;
  784.  
  785.     if ( !*ln )
  786.         return 0L;
  787.  
  788.     while ( *s && *ln && ((*s++|32)==(*ln++|32)) );
  789.  
  790.     if ( !*s && ( !*ln || *ln == ' ' || *ln == '\t' || *ln == '=' ) )
  791.         {
  792.         while( *ln && (*ln == ' ' || *ln == '\t' || *ln == '=' ) )
  793.             ln++;
  794.         if ( *ln && store )
  795.             {
  796.             char *ln2 = ln;
  797.             if ( *ln2 == 39 ) /* ' */
  798.                 {
  799.                 ln2++;
  800.                 while ( *ln2 && *ln2 != 39 )
  801.                     *store++ = *ln2++;
  802.                 }
  803.             else if ( *ln2 == 34 ) /* " */
  804.                 {
  805.                 ln2++;
  806.                 while ( *ln2 && *ln2 != 34 )
  807.                     *store++ = *ln2++;
  808.                 }
  809.             else
  810.                 while ( *ln2 && *ln2 != ' ' )
  811.                     *store++ = *ln2++;
  812.             *store = 0;
  813.             }
  814.         return ln;
  815.         }
  816.  
  817.     while ( *ln && *ln != ' ' && *ln != '\t' )
  818.         ln++;
  819.     }
  820. }
  821.  
  822.  
  823. BOOL __asm GetArgInt( register __a0 char *line, register __a1 char *hdr, register __d0 int *store )
  824. {
  825. register char *rs;
  826. register int result;
  827.  
  828. if ( rs = GetArgString( line, hdr, NULL ) )
  829.     {
  830.     result = *rs++ - '0';
  831.     while ( *rs >= '0' && *rs <= '9' )
  832.         result = 10 * result + *rs++ - '0';
  833.     *store = result;
  834.     return TRUE;
  835.     }
  836. else
  837.     return FALSE;
  838. }
  839.  
  840.  
  841.  /*********************
  842.  *                    *
  843.  * Font related stuff *
  844.  *                    *
  845.  *********************/
  846. /*
  847. extern int atoi( const char * );
  848.  
  849. void __asm ObtainAnyFont( register __a0 struct DisplayIDInformation *dinfo )
  850. {
  851. struct TextAttr *ta;
  852. struct TextFont *tf;
  853. char *buff;
  854.  
  855. if ( ta = (struct TextAttr *)AllocVec( sizeof(struct TextAttr), MEMF_ANY|MEMF_CLEAR ) )
  856.     {
  857.     ta->ta_Flags = FPF_DISKFONT;
  858.     ta->ta_YSize = 20;
  859.     if ( buff = AllocVec( 36, MEMF_ANY|MEMF_CLEAR ) )
  860.         {
  861.         ta->ta_Name = buff;
  862.         strcpy( buff, "diamond.font" );
  863.         dinfo->di_TextAttr = ta;
  864.  
  865.         if ( GetArgString( dinfo->di_Args, "FONTHEIGHT", buff ) )
  866.             ta->ta_YSize = atoi( buff );
  867.         else
  868.             ta->ta_YSize = 20;
  869.         if ( 0 == GetArgString( dinfo->di_Args, "FONTNAME", buff ) )
  870.             strcpy( buff, "diamond.font" );
  871.  
  872.         if ( tf = OpenDiskFont( ta ) )
  873.             {
  874.             dinfo->di_Font = tf;
  875.             return;
  876.             }
  877.  
  878.         strcpy( buff, "diamond.font" );
  879.         ta->ta_YSize = 20;
  880.         if ( tf = OpenDiskFont( ta ) )
  881.             {
  882.             dinfo->di_Font = tf;
  883.             return;
  884.             }
  885.  
  886.         strcpy( buff, "times.font" );
  887.         ta->ta_YSize = 24;
  888.         if ( tf = OpenDiskFont( ta ) )
  889.             {
  890.             dinfo->di_Font = tf;
  891.             return;
  892.             }
  893.  
  894.         strcpy( buff, "topaz.font" );
  895.         ta->ta_YSize = 8;
  896.         ta->ta_Flags = FPF_ROMFONT;
  897.         dinfo->di_Font = OpenFont( ta );
  898.         }
  899.     else FreeVec( ta );
  900.     }
  901. }
  902. */
  903.  
  904. #endif
  905.  
  906. #ifdef LINKED
  907. #include "//include/client_pragmas.h"
  908.  
  909.  /***********************
  910.  *                      *
  911.  * Server.lib functions *
  912.  *                      *
  913.  ***********************/
  914.  
  915. struct Library *ClientBase;
  916. static struct MsgPort *CommunicationPort;
  917.  
  918.  
  919. struct DisplayIDInformation *__asm __saveds OpenCommunication( void )
  920. {
  921. register struct DisplayIDInformation *dinfo;
  922.  
  923. if ( ClientBase = OpenLibrary( "client.library", 0L ) )
  924.     {
  925.     if ( dinfo = __OpenCommunication( &CommunicationPort ) )
  926.         return dinfo;
  927.     CloseLibrary( ClientBase );
  928.     }
  929. return NULL;
  930. }
  931.  
  932.  
  933. void __asm __saveds CloseCommunication( register __a1 struct DisplayIDInformation *dinfo )
  934. {
  935. if ( CommunicationPort ) DeleteMsgPort( CommunicationPort );
  936. if ( dinfo ) FreeVec( dinfo );
  937. if ( ClientBase ) CloseLibrary( ClientBase );
  938. }
  939.  
  940.  
  941. BOOL __asm __saveds SendClientMsg( register __d0 ULONG action )
  942. {
  943. return __SendClientMsg( action, CommunicationPort );
  944. }
  945.  
  946.  
  947. ULONG __asm __saveds GetServerCommand( void )
  948. {
  949. return __GetServerCommand( CommunicationPort );
  950. }
  951.  
  952.  
  953. ULONG __asm __saveds WaitServerCommand( void )
  954. {
  955. return __WaitServerCommand( CommunicationPort );
  956. }
  957.  
  958.  
  959. extern far struct Custom custom;
  960.  
  961. void __asm __saveds SpritesOff( void )
  962. {
  963. OFF_SPRITE
  964. custom.spr[0].ctl=0;
  965. custom.spr[1].ctl=0;
  966. custom.spr[2].ctl=0;
  967. custom.spr[3].ctl=0;
  968. custom.spr[4].ctl=0;
  969. custom.spr[5].ctl=0;
  970. custom.spr[6].ctl=0;
  971. custom.spr[7].ctl=0;
  972. }
  973.  
  974. void __asm __saveds SpritesOn( void )
  975. {
  976. ON_SPRITE
  977. }
  978.  
  979. #endif
  980.